/*
///	The User Object is the central Objekt for the ThatsMe Homepage
///	written by cansik 21.05.2010
 */

using System;
using System.Text;
using HtmlAgilityPack;
using TM.Unofficial.Chat;
using TM.Unofficial.PersonalMessageSystem;

namespace TM
{
	/// <summary>
	/// ThatsMe User Object for login procedure
	/// </summary>
	public class User : TM.TMObjects.Member
	{
		TM.API.ThatsMe thatsmehandler;
		TM.Unofficial.Network.VBrowser browser;
		
		/// <summary>
		/// Set the Password of the User
		/// </summary>
		public string Password{get; set;}
		
		/// <summary>
		/// Personal Message System
		/// </summary>
		public PMInterface PersonalMessageInterface{get; private set;}
		
		/// <summary>
		/// Chat System
		/// </summary>
		public ChatClient Chat{get; private set;}
		
		/// <summary>
		/// Check If Logged In
		/// </summary>
		public bool LoggedIn { get; private set; }
		
		/// <summary>
		/// Stealth Option for Login
		/// </summary>
		public bool Stealth { get; private set; }
		
		/// <summary>
		/// RemindMe Option for Login
		/// </summary>
		public bool RemindMe { get; private set; }
		
		/// <summary>
		/// Initializing a new User by Nickname
		/// </summary>
		/// <param name="Nick">Thats-Me Login Name</param>
		public User (string Nick)
		{
			//Initialize Objects
			thatsmehandler = new TM.API.ThatsMe();
			this.LoggedIn = false;
			browser = new Unofficial.Network.VBrowser(10000);
			
			//Fill Variabels
			TMObjects.Member tempMember = thatsmehandler.getMemberByNick(Nick);
			this.Birthday = tempMember.Birthday;
			this.Canton = tempMember.Canton;
			this.ID = tempMember.ID;
			this.Image = tempMember.Image;
			this.Nick = tempMember.Nick;
			this.Profile = tempMember.Profile;
			this.Sex = tempMember.Sex;
		}
		
		/// <summary>
		/// Login procedure at Thats-Me.ch
		/// </summary>
		/// <returns>Login Status</returns>
		public bool Login(bool RemindMe, bool Stealth)
		{
			//Setting up login options
			this.RemindMe = RemindMe;
			this.Stealth = Stealth;
			
			//Login in
			string html = browser.PostRequest(new Uri("http://thats-me.ch/login.php"), Encoding.UTF8.GetBytes("Benutzername=" + this.Nick + "&Passwort=" + this.Password + "&btn=login&merken=" + Convert.ToInt32(this.RemindMe).ToString() + "&stealth=" + Convert.ToInt32(this.Stealth).ToString()), true);

			//Searching for logout link
			if (html.Contains("href=\"/logout.php\""))
			{
				LoggedIn = true;
				
				//initialize the PMS
				this.PersonalMessageInterface = new PMInterface(this.browser);
				
				//initialize the chat
				Chat = new ChatClient(this.browser);
				
				return true;
			}
			else
			{
				LoggedIn = false;
				return false;
			}
		}

		/// <summary>
		/// To get the number of new Alerts
		/// </summary>
		/// <returns>Number of new Alerts</returns>
		public int GetNumberOfAlerts()
		{
			HtmlDocument doc = new HtmlDocument();
			int npnacount = 0;
			
			//fetching mainpage
			doc.LoadHtml(browser.GetRequest(new Uri("http://thats-me.ch/index.php"), false));
			//reading alert count
			HtmlNode npna = doc.GetElementbyId("npna");
			if (npna != null)
			{
				npnacount = Convert.ToInt32(npna.InnerText);
			}
			
			return npnacount;
		}

		/// <summary>
		/// To get the number of new Messages
		/// </summary>
		/// <returns>Number of new Messages</returns>
		public int GetNumberOfMessages()
		{
			HtmlDocument doc = new HtmlDocument();
			int npncount = 0;
			
			//fetching mainpage
			doc.LoadHtml(browser.GetRequest(new Uri("http://thats-me.ch/index.php"), false));
			//reading message count
			HtmlNode npn = doc.GetElementbyId("npn");
			if (npn != null)
			{
				npncount = Convert.ToInt32(npn.InnerText);
			}
			
			return npncount;
		}
	}
}

